{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "之前，我们讲解了在numpy中，如何通过randint、random、rand生成指定shape的随机数数组的方法。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "假如存在这样一种场景，就是我们希望将我们的notebook笔记分享给其他人，可问题是如果笔记中存在这种随机数生成的数组数据，那其他人看到的结果可能和自己就不一样。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果我们希望试验的结果不变，就必须控制随机数，既让它随机，又不能让它那么的随机，解决方法很简单，就是在生成随机数的方法前再添加一个np.random.seed方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 0, 3],\n",
       "       [3, 7, 9],\n",
       "       [3, 5, 2],\n",
       "       [4, 7, 6],\n",
       "       [8, 8, 1]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(0)\n",
    "random_array4 = np.random.randint(10,size=(5,3))\n",
    "random_array4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这个seed方法需要传入一个种子值，随便一个就行，这样无论执行多少次上面的代码，random_array4数组的值都不会变化"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Viewing arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[[6, 7, 7, 8, 1],\n",
       "         [5, 9, 8, 9, 4],\n",
       "         [3, 0, 3, 5, 0],\n",
       "         [2, 3, 8, 1, 3]],\n",
       "\n",
       "        [[3, 3, 7, 0, 1],\n",
       "         [9, 9, 0, 4, 7],\n",
       "         [3, 2, 7, 2, 0],\n",
       "         [0, 4, 5, 5, 6]],\n",
       "\n",
       "        [[8, 4, 1, 4, 9],\n",
       "         [8, 1, 1, 7, 9],\n",
       "         [9, 3, 6, 7, 2],\n",
       "         [0, 3, 5, 9, 4]]],\n",
       "\n",
       "\n",
       "       [[[4, 6, 4, 4, 3],\n",
       "         [4, 4, 8, 4, 3],\n",
       "         [7, 5, 5, 0, 1],\n",
       "         [5, 9, 3, 0, 5]],\n",
       "\n",
       "        [[0, 1, 2, 4, 2],\n",
       "         [0, 3, 2, 0, 7],\n",
       "         [5, 9, 0, 2, 7],\n",
       "         [2, 9, 2, 3, 3]],\n",
       "\n",
       "        [[2, 3, 4, 1, 2],\n",
       "         [9, 1, 4, 6, 8],\n",
       "         [2, 3, 0, 0, 6],\n",
       "         [0, 6, 3, 3, 8]]]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a4 = np.random.randint(10,size=(2,3,4,5))\n",
    "a4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果你是第一次看上面的a4，数组的size是(2, 3, 4, 5)，这看上去有些令人困惑，这个2、3、4、5和实际的数据到底是怎么对应的？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如上一层层往里看，最终便可以确定size=(2, 3, 4, 5)，也就是说size中的每一个数字，代表了当前dim维度包含的元素个数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里，我们要将size和ndim区分开来，对于size=(2, 3, 4, 5)，它的ndim是4，代表有4个维度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "好了，那下面我来出一道题目：对a4切片，获取前3个元素的数组，效果是这样的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[[6, 7, 7],\n",
       "         [5, 9, 8],\n",
       "         [3, 0, 3],\n",
       "         [2, 3, 8]],\n",
       "\n",
       "        [[3, 3, 7],\n",
       "         [9, 9, 0],\n",
       "         [3, 2, 7],\n",
       "         [0, 4, 5]],\n",
       "\n",
       "        [[8, 4, 1],\n",
       "         [8, 1, 1],\n",
       "         [9, 3, 6],\n",
       "         [0, 3, 5]]],\n",
       "\n",
       "\n",
       "       [[[4, 6, 4],\n",
       "         [4, 4, 8],\n",
       "         [7, 5, 5],\n",
       "         [5, 9, 3]],\n",
       "\n",
       "        [[0, 1, 2],\n",
       "         [0, 3, 2],\n",
       "         [5, 9, 0],\n",
       "         [2, 9, 2]],\n",
       "\n",
       "        [[2, 3, 4],\n",
       "         [9, 1, 4],\n",
       "         [2, 3, 0],\n",
       "         [0, 6, 3]]]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a4[:,:,:,:3]"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "f0eadef1d77f15b2dc417c8cc1a73e43ff71cc62600b9af0dbbd14bf3385942a"
  },
  "kernelspec": {
   "display_name": "Python 3.9.12",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
